/**
* Created on Jan 10, 2003
*
*/
package net.sf.flock.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
* net.sf.flock.parser
* Created at Jan 10, 2003 in URLLoader.java
* @author zsombor.gegesy
class URLLoader {
private final static Logger LOGGER = LogManager.getLogger(URLLoader.class);
private URL url;
private String encoding;
private byte[] result;
public URLLoader(URL url) {
this.url = url;
}
public void load() throws IOException {
URLConnection connection = url.openConnection();
InputStream input = connection.getInputStream();
encoding = connection.getContentEncoding();
LOGGER.debug("open connection to "+url);
ByteArrayOutputStream baos = new ByteArrayOutputStream(16384);
byte[] buf = new byte[1024];
try {
int len = 0;
do {
len = input.read(buf);
LOGGER.debug("read "+len+" bytes");
if (len>0)
baos.write(buf,0,len);
} while(len>0);
} catch (IOException io) {
LOGGER.debug(io);
result = baos.toByteArray();
public String getAsString() throws UnsupportedEncodingException {
return getAsString(getEncoding());
public String getAsString(String encod) throws UnsupportedEncodingException {
if (encod == null) {
encod = "UTF-8";
return new String(result, encod);
public Reader getAsReader() throws UnsupportedEncodingException {
return getAsReader(getEncoding());
public Reader getAsReader(String encod) throws UnsupportedEncodingException {
return new StringReader(getAsString(encod));
public InputStream getAsInputStream() {
return new ByteArrayInputStream(result);
public String getEncoding() {
return encoding;
public void setEncoding(String encoding) {
this.encoding = encoding;